<template>
    <div class="device-wrapper">
        <Block>
            <DataGrid :auto-load="autoLoad" row-key="id" border ref="dataGridRef">
                <template #toolbar-search>
                    <div class="query-from-box">
                        <el-form>
                            <el-form-item>
                                <el-input v-model="queryData.deviceKey" placeholder="设备名称/标识符/编号" clearable />
                            </el-form-item>
                            <el-form-item>
                                <el-tree-select
                                    placeholder="设备标签"
                                    v-model="queryData.labels"
                                    :data="labelTree"
                                    :props="defaultProps"
                                    multiple
                                    collapse-tags
                                    collapse-tags-tooltip
                                    check-strictly
                                    :render-after-expand="false"
                                    show-checkbox
                                    clearable
                                    node-key="labelNo"
                                    @visible-change="getLabelTree"
                                />
                            </el-form-item>
                            <el-form-item>
                                <el-select v-model="queryData.enabled" placeholder="启用状态" clearable>
                                    <el-option
                                        v-for="(value, key) in ProductState"
                                        :label="value"
                                        :key="key"
                                        :value="key"
                                    />
                                </el-select>
                            </el-form-item>
                            <el-form-item>
                                <el-select placeholder="在线状态" v-model="queryData.deviceStatus" clearable>
                                    <el-option
                                        v-for="(value, key) in DeviceStatus"
                                        :label="value"
                                        :key="key"
                                        :value="key"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-form>
                    </div>
                </template>
                <el-table-column type="index" label="序号" width="60" align="center" fixed />
                <el-table-column label="设备名称" width="300" fixed>
                    <template #default="scope">
                        <el-button link type="primary" @click="onGoDetail(scope.row)">
                            {{ scope.row.deviceName }}
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="id" label="标识符" show-overflow-tooltip />
                <el-table-column prop="deviceNo" label="设备编号" show-overflow-tooltip />
                <el-table-column label="设备标签">
                    <template #default="scope">
                        {{ scope.row.labelNames ? scope.row.labelNames.join('；') : '' }}
                    </template>
                </el-table-column>
                <el-table-column label="节点类型" width="95">
                    <template #default="scope">
                        <div class="table-item-tag">
                            <el-tag
                                :type="
                                    scope.row.productType == 'DIRECT'
                                        ? 'success'
                                        : scope.row.productType == 'GATEWAY'
                                          ? 'primary'
                                          : 'warning'
                                "
                                >{{ ProductType[scope.row.productType] }}</el-tag
                            >
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="连网信息" show-overflow-tooltip width="180">
                    <template #default="scope">
                        <template v-if="scope.row.nbIotSerial || scope.row.nbIotRegistration">
                            NB-IoT串口号{{ scope.row.nbIotSerial || '--' }};注册{{
                                scope.row.nbIotRegistration || '--'
                            }}
                        </template>
                        <template
                            v-else-if="scope.row.networkIp || scope.row.networkPort || scope.row.networkPort == 0"
                        >
                            IP地址{{ scope.row.networkIp || '--' }}:{{
                                scope.row.networkPort || scope.row.networkPort == 0 ? scope.row.networkPort : '--'
                            }}
                        </template>
                        <template v-else-if="scope.row.serialPort || scope.row.baudRate">
                            串口号{{ scope.row.serialPort || '--' }}、波特率{{ scope.row.baudRate || '--' }}
                        </template>
                        <template v-else>--</template>
                    </template>
                </el-table-column>
                <el-table-column label="启用状态" width="65">
                    <template #default="scope">
                        <span class="table-item-status" :status="scope.row.enabled ? '启用' : '停用'">
                            {{ scope.row.enabled ? '启用' : '停用' }}
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="在线状态" width="65">
                    <template #default="scope">
                        <span class="table-item-status" :status="DeviceStatus[scope.row.deviceStatus]">
                            {{ DeviceStatus[scope.row.deviceStatus] }}
                        </span>
                    </template>
                </el-table-column>
            </DataGrid>
        </Block>
    </div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { IoTDeviceService, runLabelService } from '@great/services';
import { useRouter } from 'vue-router';
import { ProductType, DeviceStatus, REQUEST_CODE_MESSAGE } from '@/utils/constant';
import { DataGrid, Block, type DataGridInstance } from '@great/components';
import { checkPermi } from '@/utils/ruoyi';

const router = useRouter();
enum ProductState {
    true = '启用',
    false = '停用'
}
const props = defineProps<{
    detailData?: Record<string, any>;
}>();
// 查询条件
const queryData = ref({
    productNo: props.detailData?.productNo,
    labels: [],
    enabled: '',
    deviceStatus: '',
    deviceKey: ''
});
const defaultProps = {
    children: 'childLabel',
    label: 'labelName',
    value: 'labelNo'
};
const labelTree = ref();
const dataGridRef = useTemplateRef<DataGridInstance>('dataGridRef');
const autoLoad = reactive({
    debounce: true,
    reloadDeps: [queryData],
    onLoad: async (current, size) => {
        const res = await IoTDeviceService.getPageDevice({
            current,
            size,
            labels: queryData.value.labels ? queryData.value.labels.join(',') : null,
            ...queryData.value
        });
        return { data: res.data.records, total: res.data.total };
    }
});

// 查看详情
const onGoDetail = (row: { id: number }) => {
    if (checkPermi(['iot:device:detail'])) {
        router.push({
            name: 'IoT-device-detail',
            params: {
                id: row?.id
            }
        });
    }
};
// 获取标签
const getLabelTree = async () => {
    if (labelTree.value && labelTree.value.length > 0) return;
    const res = await runLabelService.getLabelTree({ labelTypeNo: 'labelType-deviceLabel' });
    labelTree.value = res.data;
};
</script>
<style scoped lang="scss">
.device-wrapper {
    .query-from-box {
        display: flex;
        align-items: center;
        .el-form {
            display: flex;
            .el-form-item {
                margin-bottom: 0;
                width: 192px;
            }
            .el-form-item + .el-form-item {
                margin-left: 10px;
            }
        }
    }
    :deep(.data-grid) {
        .table-item-tag {
            display: flex;
        }
        .table-item-status {
            display: flex;
            align-items: center;
            &::before {
                content: '';
                width: 6px;
                height: 6px;
                display: inline-block;
                border-radius: 50%;
                margin-right: 5px;
                background-color: #e6a23c;
            }
        }
        .table-item-status[status='停用'],
        .table-item-status[status='无'] {
            &::before {
                background-color: #909399;
            }
        }
        .table-item-status[status='启用'],
        .table-item-status[status='在线'] {
            &::before {
                background-color: #67c23a;
            }
        }
        .table-item-status[status='离线'] {
            &::before {
                background-color: #f56c6c;
            }
        }
    }
}
</style>
